﻿using System;
using System.Collections.Generic;
using System.Text;
using System.ServiceModel;
using System.IO;
using System.IO.Compression;
using MM.Common.IRemoteDataBase;
using System.ServiceModel.Channels;

namespace MM.Common.DBBase
{
    internal class RemoteQuery : ClientBase<IQueryService>, IQueryService
    {
        // Methods
        internal RemoteQuery()
        {
        }

        internal RemoteQuery(string endpointConfigurationName): base(endpointConfigurationName)
        {
        }

        internal RemoteQuery(Binding binding, EndpointAddress address): base(binding, address)
        {
        }

        protected string GzipDeCompress(byte[] bytInput)
        {
            StringBuilder builder = new StringBuilder();
            int num = 0;
            byte[] buffer = new byte[0x1000];
            try
            {
                Stream stream = new GZipStream(new MemoryStream(bytInput), CompressionMode.Decompress);
                while (true)
                {
                    int count = stream.Read(buffer, 0, buffer.Length);
                    if (count <= 0)
                    {
                        break;
                    }
                    num += count;
                    builder.Append(Encoding.Unicode.GetString(buffer, 0, count));
                }
                stream.Close();
            }
            catch
            {
            }
            return builder.ToString();
        }

        public bool RemoteDBExecuteScalar(string rcode, ref string error, string commandtext, OraParm[] parm, ref string obj)
        {
            try
            {
                if (!base.Channel.RemoteDBExecuteScalar(rcode, ref error, commandtext, parm, ref obj))
                {
                    error = "服务端错误:" + error;
                    return false;
                }
                return true;
            }
            catch (Exception exception)
            {
                error = "接口端错误:" + exception.Message;
                return false;
            }
        }

        public string RemoteDBRetrieve(string rcode, ref string error, string commandtext, OraParm[] parm, string table_name)
        {
            try
            {
                string s = base.Channel.RemoteDBRetrieve(rcode, ref error, commandtext, parm, table_name);
                if (s == string.Empty)
                {
                    error = "服务端错误:" + error;
                    return s;
                }
                byte[] bytInput = Convert.FromBase64String(s);
                return this.GzipDeCompress(bytInput);
            }
            catch (Exception exception)
            {
                error = "接口端错误:" + exception.Message;
                return string.Empty;
            }
        }

        public string[] RemoteDBRetrieveArray(string rcode, ref string error, string[] commandtexts, OraParm[][] parms, string[] table_names)
        {
            try
            {
                string[] strArray = new string[commandtexts.Length];
                for (int i = 0; i < commandtexts.Length; i++)
                {
                    string s = base.Channel.RemoteDBRetrieve(rcode, ref error, commandtexts[i], parms[i], table_names[i]);
                    if (s == string.Empty)
                    {
                        error = "服务端错误:" + error;
                        break;
                    }
                    byte[] bytInput = Convert.FromBase64String(s);
                    strArray[i] = this.GzipDeCompress(bytInput);
                }
                return strArray;
            }
            catch (Exception exception)
            {
                error = "接口端错误:" + exception.Message;
                return new string[0];
            }
        }

        public string RemoteDBRetrieveProcedure(string rcode, ref string error, string procedure_name, ref OraParm[] parm, string cursor_name, string table_name)
        {
            try
            {
                string s = base.Channel.RemoteDBRetrieveProcedure(rcode, ref error, procedure_name, ref parm, cursor_name, table_name);
                if (s == string.Empty)
                {
                    error = "服务端错误:" + error;
                    return s;
                }
                byte[] bytInput = Convert.FromBase64String(s);
                return this.GzipDeCompress(bytInput);
            }
            catch (Exception exception)
            {
                error = "接口端错误:" + exception.Message;
                return string.Empty;
            }
        }

        public bool RemoteRegister(ref string rcode, ref string error, string node, string password)
        {
            try
            {
                if (base.Channel.RemoteRegister(ref rcode, ref error, node, password))
                {
                    return true;
                }
                error = "服务端错误:" + error;
                return false;
            }
            catch (Exception exception)
            {
                error = "接口端错误: " + exception.Message;
                return false;
            }
        }
    }
}
